// package_work/pages/addWork/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    barHeight:40,
    imgUrl:'/images/icons/img.png',
    status:'',
    list:{
      "leave":"申请请假",
      "repair":"工单保修",
      "other":"其他类型"
    },
    personnel:[],
    maintenance:[],
    textareaValue: ''
  },
  goBack(){
    wx.navigateBack()
  },
  handleUpload(){
    wx.chooseMedia({
      success:(media)=>{
        console.log('选取本地图像资源',media);
        let tempUrl=media.tempFiles[0].tempFilePath
        // this.setData({imgUrl:tempUrl})
        let str=Math.random().toString(36).substring(2)
        let time=Date.now()
        let cloudPath=str+time+'.png'
        wx.cloud.uploadFile({
          filePath:tempUrl,
          cloudPath,
          success:(res)=>{
            console.log('云存储后下发的路径',res);
            this.setData({
              imgUrl:res.fileID,
              value:res.fileID
            })
            this.triggerEvent('uploaded',res.fileID)
          }
        })
      }
    })
  },
  textareaAInput(e) {
    this.setData({
      textareaValue: e.detail.value
    });
  },
async  submitForm(e){
    console.log('获取反馈信息',e);
    let res=e.detail.value;
    console.log('获取表单信息',res);
    console.log('链接',this.data.imgUrl);
    let time=Date.now()
    const date = new Date(time);
    console.log('时间',time);
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    const hours = String(date.getHours()).padStart(2, '0');
    const minutes = String(date.getMinutes()).padStart(2, '0');
    const seconds = String(date.getSeconds()).padStart(2, '0');
    const formattedTime = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
    console.log(formattedTime);
    let completeTime='';
    // 提取指派人信息
    let assignee=''
    const { maintenance, personnel } = this.data;
    if(maintenance.length!=0){
      assignee=maintenance[res.assignee]
    }else {
      assignee=personnel[res.assignee]
    }
    console.log('指派人',assignee);
    //状态
    let status=''
    if(!assignee){
      status='待接单'
    }else{
      status='待处理'
    }
    console.log('当前订单状态',status);
    //工单类型
    let category=''
    if(this.data.status==='leave'){
      category="请假"
    }else if(this.data.status==='repair'){
      category="工单报障"
    }else{
      category="其他类型"
    }
    console.log('类型',category);
    await getApp().db.collection('work').add({
      data:{
        address:'四川泸州市龙泉驿区',
        assignee,
        category,
        completeTime,
        createTime:formattedTime,
        creator:'林楠',
        details:res.details,
        images:this.data.imgUrl,
        status,
        title:res.title
      }
    })
    wx.navigateBack()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    let info=wx.getWindowInfo()
    let res =await getApp().db.collection('user').get()
    console.log('用户表',res.data);
    let maintenance=[];
    let personnel=[]
    for(let i=0;i<res.data.length;i++){
      if(res.data[i].department==='维修部'){
        maintenance.push(res.data[i].username)
      }else if(res.data[i].department==='人事部'){
        personnel.push(res.data[i].username)
      }
    }
    console.log('路由所带参数',options);
    let status=options.status
    console.log(info);
    this.setData({
      barHeight:info.statusBarHeight*2-20,
      status,
      personnel,
      maintenance
    })
  },
})